﻿#ifndef FRMWEBCLIENT_H
#define FRMWEBCLIENT_H

#include <QWidget>
#include <QtWebSockets>
#include <thread>
#include <memory.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include "common/keyboardmanager/KeyBoardManger.h"
#include "api/udpclient.h"
#include "common/utilities/datamanager.h"
#include "common/siyi/siyicameracontrol.h"
#include <QImage>
#include <QMessageBox>
#include <QDebug>
#include <QPainter>
#include <QPaintEvent>
#include<QMouseEvent>
#include "play/readthread.h"
namespace Ui {
class frmWebClient;
}

class frmWebClient : public QWidget
{
    Q_OBJECT

public:
    explicit frmWebClient(QWidget *parent = 0);
    ~frmWebClient();
    void DoDetectPro();

    void adjustCirclesPositionAndSize(); //根据窗体大小调整位置和大小
protected:
    bool eventFilter(QObject *watched, QEvent *event);

private:
    Ui::frmWebClient *ui;


private:
    void startFtpHandlerProc(void); //启动Ftp服务进程
    void HandlerIsExistProc(void);
    bool IsProcessRunning(const std::string& processName);
    void drawRedCircle(QPixmap *pixmap, const QPoint &center, int radius);
    QPixmap loadAndModifyImage(const QString &filePath, const QPoint &circleCenter, int radius);
    PositionData parsePositionData(const QString &jsonString);
    void onShowAlarm();
    //发送消息
    void SendMsg(const QString& msg);
    void init(); //初始化配置
private:
    void paintEvent(QPaintEvent *event) override; //绘图事件：绘制地图、摇杆中的大圆和小圆
    void mouseMoveEvent(QMouseEvent *) override; //鼠标移动事件：实现摇杆功能
    void mousePressEvent(QMouseEvent *) override; //鼠标按下事件：获取摇杆所在的实时坐标
    void mouseReleaseEvent(QMouseEvent *) override; //鼠标释放事件：释放鼠标，则MousePressFlag复位，且小圆圆心设置为初始值，更新绘图事件后，重新绘图使小圆（摇杆）回到原处
    void resizeEvent(QResizeEvent *event) override; // 添加重写方法
private slots:
    void initForm();
    void initConfig();
    void saveConfig();
    void initTimer();
    void append(int type, const QString &data, bool clear = false);
private slots:
    void connected();
    void disconnected();
    void error();
    void sendData(const QString &data);
    void openFolder();

    void textFrameReceived(const QString &data, bool isLastFrame);
    void binaryFrameReceived(const QByteArray &data, bool isLastFrame);
    void textMessageReceived(const QString &data);
    void binaryMessageReceived(const QByteArray &data);
    void onSendMsg(const QString  &msg);

private slots:
    void on_btnConnect_clicked();
    void on_btnSave_clicked();
    void on_btnClear_clicked();
    void on_btnShowAlarm_clicked();
    void on_btnStartPatrol_clicked();

    void on_btnHandlerCtrl_clicked();

private slots:
    //选择视频文件按钮的槽函数
    void on_but_file_clicked();
    //开始播放按钮的槽函数
    void on_but_open_clicked();
    //暂停播放按钮的槽函数
    void on_btn_pause_clicked();
    //自定义槽函数，用于处理页面播放状态
    void on_playState(readThread::PlayState state);
private:
    QPoint m_RightBigCir_xy; //大圆圆心坐标
    QPoint m_RightSmallCir_xy; //小圆圆心坐标

    QPoint m_LeftBigCir_xy; //左边大圆圆心坐标
    QPoint m_LeftSmallCir_xy; //左边小圆圆心坐标
    bool  m_MouseRightFlag; //鼠标按下的标志位
    bool  m_MouseLeftFlag; //遥感左键看下


    QPoint m_MapRemov_Old;
    QPoint m_SmallCir_xy, m_BigCir_xy; //设置成员变量
    QPixmap m_RightbigCircle_Pixmap;
    QPixmap m_RightsmallCircle_Pixmap;
    QPixmap m_LeftbigCircle_Pixmap;
    QPixmap m_LeftsmallCircle_Pixmap;
    int m_bigCircleRadius; // 大圆半径
    int m_smallCircleRadius; // 小圆半径
    //维持视频解码线程
    readThread* m_readThread = NULL;
    //视频解码红外解码线程
    readThread* m_InfraredThread = NULL;
private:
    bool isOk;
    QWebSocket *socket;
    QTimer *timer;
    bool m_isStart; //判断服务端是否已经启动
    std::thread m_handler;
    std::thread m_isRun;//线程判断是否已经起来
    KeyBoardManger *m_keyManager;
    UdpClient *m_client;
    QImage image;  // 用于加载和修改的图片
    //创建map,将id作为key, buffer作为value
    std::map<int, QString> m_bufMap;
    //记录Id
    int m_id;
    bool m_isAlarm; // 有告警记录一次
    QString m_pathAlarmFile; //告警图片位置
    QString m_imagePath;
    QString m_cmd; //操控命令
    bool m_isHandCtrl;
    UdpClient *m_handClient; //主要用于巡检和手柄控制
    SiYiCameraControl *m_SiYiCameraCtl;
};

#endif // FRMWEBCLIENT_H
